import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
/**
 * 
 *Gui of add Update Book's Subjects window for the user's use
 */
public class UpdateBookSubject implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JLabel Subjects=null;
	private JLabel BookName=null;
	private JLabel AddASub=null;
	public JList SubjectsList = null;
	public JButton add = null;
	private JButton remove = null;
	public JComboBox SubjectCombo2 = null;
	public JComboBox FieldCombo2 = null;
	public JButton ViewFields = null;
	public JButton ClearSel = null;
	private JButton cancel = null;  
	private JButton logout1 = null;
	public JPanel panel=new JPanel();
	/** The Wanted Book's Serial*/
	public String BookSerial;
	/** The Wanted Book's Subjects*/
	public String[] Arr;

	/**
	 * This is the default constructor + call to Database to get Book's Subjects
	 */
	public UpdateBookSubject(String Serial) {
		super();
		BookSerial=Serial;
		Arr=Main.UpdBooksSubjectCTRL.o_GetBooksSubjects(Serial);
		initialize();
	}	

	/**
	 * This method initializes Update Book's Subjects Gui
	 * 
	 * @return void
	 */
	public void initialize() {		
		SubjectCombo2= new JComboBox (Main.UpdBooksSubjectCTRL.o_GetSubjects());
		SubjectCombo2.setBounds(300, 90, 150, 36);
		panel.add(SubjectCombo2,null);
		panel.add(getViewFields(), null);
		panel.add(getClearSelect(), null);
		
		BookName=new JLabel("Book name : "+Main.UpdBooksSubjectCTRL.BookName +" (Serial:"+BookSerial+")");
		BookName.setBounds(new Rectangle(20, 10, 300, 23));		
		Subjects=new JLabel("    Field       :       Subject");
		Subjects.setFont(new Font("David",Font.ITALIC,14));
		Subjects.setForeground(Color.GRAY);
		Subjects.setBounds(new Rectangle(45, 41, 240, 23));
		AddASub=new JLabel("Add a Subject:");
		AddASub.setFont(new Font("David",Font.BOLD,18));
		AddASub.setBounds(new Rectangle(310, 65, 240, 23));
		panel.setLayout(null);
		panel.add(getAdd(), null);
		panel.add(getRemove(), null);
		panel.add(getCancel(),null);
		panel.add(getLogout1(), null);
		panel.add(Subjects);
		panel.add(BookName);
		panel.add(AddASub);
		panel.add(getSubjectsList(), null);
		panel.setEnabled(false);
		panel.setVisible(false);
	}

	/**
	 * Initializes Subjects List with the data received from database (Arr)
	 * 
	 * @return javax.swing.JList	
	 */
	public JList getSubjectsList() {
		if (SubjectsList == null) {
			SubjectsList = new JList(Arr);
			SubjectsList.setBounds(new Rectangle(40, 58, 210, 170));
			SubjectsList.setBorder(BorderFactory.createLineBorder(Color.black));
			SubjectsList.setBackground(Color.LIGHT_GRAY);		
			SubjectsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
		}
		return SubjectsList;
	}
	/**
	 * Initializes Remove Button and set listener on this button
	 * 
	 * @return javax.swing.JList	
	 */	
	private JButton getRemove() {
		if (remove == null) {
			remove = new JButton("Remove a Subject");
			remove.setBounds(new Rectangle(49, 240, 190, 35));
			//remove.setEnabled(false);
			remove.addActionListener(this);
		}
		return remove;
	}
	/**
	 * Initializes ClearSelect Button and set listener on this button
	 * 
	 * @return javax.swing.JButton	
	 */	
	public JButton getClearSelect() 
	{
		if (ClearSel == null) {
			ClearSel = new JButton("UnSelect");
			ClearSel.setBounds(new Rectangle(435, 190, 85, 36));
			ClearSel.addActionListener(this);	
			ClearSel.setVisible(false);
		}
		return ClearSel;
	}
	/**
	 * Initializes ViewFields Button and set listener on this button
	 * 
	 * @return javax.swing.JButton	
	 */	
	public JButton getViewFields() 
	{
		if (ViewFields == null) {
			ViewFields = new JButton("View related Fields");
			ViewFields.setBounds(new Rectangle(300, 130, 150, 36));
			ViewFields.addActionListener(this);				
		}
		return ViewFields;
	}
	/**
	 * Initializes Add Button and set listener on this button
	 * 
	 * @return javax.swing.JButton	
	 */	
	private JButton getAdd() {
		if (add == null) {
			add = new JButton("Add new subject");
			add.setBounds(new Rectangle(300, 240, 190, 35));
			add.addActionListener(this);
			add.setEnabled(false);
		}
		return add;
	}
	/**
	 * Initializes Cancel Button and set listener on this button
	 * 
	 * @return javax.swing.JButton	
	 */	
	
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton("Back");
			cancel.setBounds(new Rectangle(330, 8, 74, 24));
			cancel.addActionListener(this);
		}
		return cancel;
	}
	/**
	 * Initializes Logout Button and set listener on this button
	 * 
	 * @return javax.swing.JButton	
	 */	
	JButton getLogout1() {
		if (logout1 == null) {
			logout1 = new JButton("Logout");
			logout1.setBounds(new Rectangle(414, 8, 74, 24));
			logout1.addActionListener(this);
		}
		return logout1;
	}
	
	public void actionPerformed(ActionEvent ae)
	{					
		if(ae.getSource()==remove)
		{
			Main.UpdBooksSubjectCTRL.o_RemoveSubject(this);
		}
		
		if(ae.getSource()==ViewFields)
		{	
			if(((String)SubjectCombo2.getSelectedItem()).equals(" "))
	    	{
	    		 JOptionPane.showMessageDialog(null,"Select a Subject","Input error",JOptionPane.ERROR_MESSAGE);
	    		 return;
	    	}
			else
			{
				ViewFields.setEnabled(false);
				SubjectCombo2.setEnabled(false);				
				FieldCombo2 = new JComboBox(Main.UpdBooksSubjectCTRL.o_GetFields());				
				FieldCombo2.setBounds(new Rectangle(300, 190, 130, 36));
				panel.add(FieldCombo2, null);
				ClearSel.setVisible(true);
				add.setEnabled(true);
				FieldCombo2.setVisible(true);
			}
		}
		
		if(ae.getSource()==ClearSel)
		{
			ViewFields.setEnabled(true);
			SubjectCombo2.setEnabled(true);
			FieldCombo2.setVisible(false);
			panel.remove(FieldCombo2);	
			ClearSel.setVisible(false);
			add.setEnabled(false);
		}
		
		if(ae.getSource()==add)
		{
			Main.UpdBooksSubjectCTRL.o_AddSubject(this);
		}
		
		if(ae.getSource()==cancel)			
			Main.UpdBooksSubjectCTRL.o_Back(this.panel);	
		
		if(ae.getSource()==logout1)				
			Main.UpdBooksSubjectCTRL.o_logout(this.panel);
	}	

}  
